import matplotlib.pyplot as plt
import numpy as np


def roll_dice():
    # 模拟投掷一个六面骰子
    return np.random.randint(1, 7)


def experiment(num_rolls):
    # 进行一次试验，实验包括num_rolls 投掷骰子
    total = 0
    for _ in range(num_rolls):
        total += roll_dice()
    return total / num_rolls


def run_experiments(num_experiments, num_rolls):
    # 运行多次实验，并记录平均值
    averages = []
    for _ in range(num_experiments):
        averages.append(experiment(num_rolls))
    return averages


def plot_histogram(averages):
    # 绘制实验平均值的直方图
    plt.hist(averages, bins=20, edgecolor='black', density=True)
    plt.xlabel('Average of Rools')
    plt.ylabel('Frequency')
    plt.title('Central  Limit  Theorem Demonstration')
    plt.show()


# 参数设置  实验次数
num_experiments = 10000
# 每次实验骰子次数
num_rolls = 30

averages = run_experiments(num_experiments, num_rolls)
plot_histogram(averages)
